package com.newenergy.wisehome.gateway.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.newenergy.wisehome.R;
import com.newenergy.wisehome.data.bean.Family;


public class ManageBLFamilyAdapter extends RecyclerArrayAdapter<Family> {


    private ManageBLFamilyListener mListener;

    public ManageBLFamilyAdapter(Context context) {
        super(context);
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int
            viewType) {
        return new ViewHolder(parent);
    }

    private class ViewHolder extends BaseViewHolder<Family> {
        private TextView tvTitle;
        private Button btn_bind;
        private Button btn_unbind;

        public ViewHolder(ViewGroup parent) {
            super(parent, R.layout.item_manage_bl_family);
            tvTitle = $(R.id.tv_name);
            btn_bind = $(R.id.btn_bind);
            btn_unbind = $(R.id.btn_unbind);

            btn_bind.setOnClickListener(view -> {
                int position = getDataPosition();
                if (mListener != null) {
                    mListener.onClickBindItem(position);
                }
            });
            btn_unbind.setOnClickListener(view -> {
                int position = getDataPosition();
                if (mListener != null) {
                    mListener.onClickUnBindItem(position);
                }
            });
        }
        @Override
        public void setData(Family data) {
            super.setData(data);
             tvTitle.setText(data.getName());
             boolean hideBindBtn =!TextUtils.isEmpty(data.getBlFamilyId());
             btn_bind.setVisibility(hideBindBtn ? View.GONE:View.VISIBLE);
             btn_unbind.setVisibility(hideBindBtn? View.VISIBLE:View.GONE);
        }
    }

    public interface  ManageBLFamilyListener{
        void onClickBindItem(int position);
        void onClickUnBindItem(int position);
    }

    public void setManageBLFamilyListener(ManageBLFamilyListener listener){
        mListener = listener;
    }

}
